﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class MessageController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: Message
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Search(int page, int limit, string name, string begin, string end)
        {
            //db.Configuration.LazyLoadingEnabled = false;

            var q = db.Message.Where(x => true);

            //这里实现多条件查询
            if (name != "" && name != null)
            {
                q = q.Where(p => p.name.Contains(name));
            }
            if (!string.IsNullOrWhiteSpace(begin))
            {
                var b = Convert.ToDateTime(begin);
                q = q.Where(x => x.create_time >= b);//开始时间
            }
            if (!string.IsNullOrWhiteSpace(end))
            {
                var e = Convert.ToDateTime(end);
                q = q.Where(x => x.create_time <= e);//结束时间
            }

            var count = q.Count();//得到总统计数
            //根据时间排序，并实现分页
            var list = q.OrderByDescending(x => x.id).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
            {
                id = x.id,
                content = x.content,
                name = x.name,
                
                phone = x.phone,
                QQ = x.QQ,
                create_time = x.create_time.ToString(),
                title = x.title,
                email = x.email,

            });
            return Json(new { code = 0, msg = "", count = count, data = list });
        }
        [HttpGet]
        public ActionResult xingqing(int id)
        {
             
            {
                var info = db.Message.SingleOrDefault(p => p.id == id);
                ViewBag.A = info;
            }
            return View();
        }
        [HttpPost]
        public ActionResult xingqing(Message message)
        {
            var obj = db.Message.SingleOrDefault(x => x.id == message.id);//获取指定Id的新闻对象
            obj.content = message.content;
            obj.name = message.name;
            obj.phone = message.phone;
            obj.QQ = message.QQ;
            obj.email = message.email;
            obj.title = message.title;
            
            obj.create_time = message.create_time;

            int r = db.SaveChanges();
            if (r > 0)
            {
                return Json(new { suc = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { suc = true, msg = "编辑失败" });

            }
        }
        public ActionResult Delect(int id)
        {
             
            {
                var info = db.Message.SingleOrDefault(p => p.id == id);
                db.Message.Remove(info);
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Json(new { code = 1, msg = "删除失败" });
                }
            }

        }
    }
}